package com.handydata.antifraud.service.impl;

import com.handydata.antifraud.bo.apptypebasedata.AppTypeBasedataBeanBO;
import com.handydata.antifraud.bo.apptypebasedata.AppTypeBasedataReqBO;
import com.handydata.antifraud.bo.apptypebasedata.AppTypeBasedataResBO;
import com.handydata.antifraud.common.exception.AppTypeBasedataExCode;
import com.handydata.antifraud.dao.AppTypeBasedataDao;
import com.handydata.antifraud.entity.AppTypeBasedataDO;
import com.handydata.antifraud.service.AppTypeBasedataService;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.exception.SysException;
import com.handydata.common.service.AbsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("appTypeBasedataService")
public class AppTypeBasedataServiceImpl extends AbsService implements AppTypeBasedataService {
	
	private static final Logger logger = LoggerFactory.getLogger(AppTypeBasedataServiceImpl.class);
	
	@Autowired
	private AppTypeBasedataDao appTypeBasedataDao;

	@Transactional
	@Override
	public AppTypeBasedataResBO createAppTypeBasedata(AppTypeBasedataReqBO req) throws SysException{
	    AppTypeBasedataResBO res = new AppTypeBasedataResBO();
		try {
			AppTypeBasedataDO entity = CommonBeanConvertor.convert(req, AppTypeBasedataDO.class);
			long status = appTypeBasedataDao.insert(entity);
			if(status == 0) {
				res.setExCode(AppTypeBasedataExCode.SYS_NO_ENTITY_CREATED);
			} else {
				res.setExCode(AppTypeBasedataExCode.SUCCESS);
			}
 			
		} catch (Exception e) {
			logger.error("Create new entity {} get into error", req, e);
			throw new SysException(AppTypeBasedataExCode.SYS_CREATE_ENTITY_ERROR);
		}
		
		return res;
	}

	@Transactional
	@Override
	public AppTypeBasedataResBO modifyAppTypeBasedata(AppTypeBasedataReqBO req) throws SysException{
	    AppTypeBasedataResBO res = new AppTypeBasedataResBO();
		try {
			AppTypeBasedataDO condition = CommonBeanConvertor.convert(req, AppTypeBasedataDO.class);
			long status = appTypeBasedataDao.update(condition);
			if(status == 0) {
				res.setExCode(AppTypeBasedataExCode.SYS_NO_ENTITY_MODIFIED);
			} else {
				res.setExCode(AppTypeBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Modify entity {} get into error", req, e);
			throw new SysException(AppTypeBasedataExCode.SYS_MODIFY_ENTITY_ERROR);
		} 
		return res;
	}

	@Transactional
	@Override
	public AppTypeBasedataResBO deleteAppTypeBasedata(AppTypeBasedataReqBO req) throws SysException{
	    AppTypeBasedataResBO res = new AppTypeBasedataResBO();
		
		try {
			AppTypeBasedataDO condition = CommonBeanConvertor.convert(req, AppTypeBasedataDO.class);
			long status = appTypeBasedataDao.delete(condition);
			if(status == 0) {
				res.setExCode(AppTypeBasedataExCode.SYS_NO_ENTITY_DELETED);
			} else {
				res.setExCode(AppTypeBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Delete entity {} get into error", req, e);
			throw new SysException(AppTypeBasedataExCode.SYS_DELETE_ENTITY_ERROR);
		}
		return res;
	}

	@Override
	public AppTypeBasedataResBO queryAppTypeBasedata(AppTypeBasedataReqBO req) throws SysException{
		AppTypeBasedataDO condition = CommonBeanConvertor.convert(req, AppTypeBasedataDO.class);
		AppTypeBasedataResBO res;
		
		try {
			List<AppTypeBasedataDO> entities = appTypeBasedataDao.query(condition);
			res = CommonBeanConvertor.convert(entities, AppTypeBasedataResBO.class, AppTypeBasedataBeanBO.class);
		} catch(Exception e) {
			logger.error("Query entity {} get into error", req, e);
			throw new SysException(AppTypeBasedataExCode.SYS_QUERY_ENTITY_ERROR);
		}
		return res;
	}
	
}
